package cn.iocoder.yudao.module.system.service.investment;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentInsertReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.app.investment.vo.AppInvestmentPageReqVO;
import cn.iocoder.yudao.module.system.convert.investment.InvestmentToResp;
import cn.iocoder.yudao.module.system.dal.dataobject.investment.SystemInvestment;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.dal.mysql.investment.InvestmentMapper;
import cn.iocoder.yudao.module.system.dal.mysql.merchant.MerchantMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 梁富贵
 * @Date 2024/10/10
 **/
@Service
public class InvestmentServiceImpl implements InvestmentService {

    @Resource
    private InvestmentMapper investmentMapper;
    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private FileApi fileApi;


    /**
     * 获取招商项目列表
     * @param reqVO
     * @return
     */
    @Override
    public PageResult<InvestmentRespVO> getInvestmentPage(InvestmentReqVO reqVO) {
        PageResult<SystemInvestment> pageResult = investmentMapper.selectPage(reqVO);
        List<Long> merchantIds = new ArrayList<>();
        pageResult.getList().parallelStream().forEach(item -> merchantIds.add(item.getMerchantId()));
        List<MerchantDO> merchantDOPageResult = merchantMapper.selectList(merchantIds);

        return this.getMerchantName(merchantDOPageResult, pageResult);
    }

    /**
     * 给返回对象添加商家名称和联系地址
     *
     * @param merchantDOs
     * @param systemInvestment
     * @return
     */
    @Override
    public PageResult<InvestmentRespVO> getMerchantName(List<MerchantDO> merchantDOs, PageResult<SystemInvestment> systemInvestment) {
        Map<Long, String> merchantNameMap = merchantDOs.parallelStream().collect(
                Collectors.toMap(MerchantDO::getMerchantId, MerchantDO::getMerchantName));

        Map<Long, String> address = merchantDOs.parallelStream().collect(
                Collectors.toMap(MerchantDO::getMerchantId, MerchantDO::getContactAddress));

        PageResult<InvestmentRespVO> result = new PageResult<>();
        result.setTotal(systemInvestment.getTotal()).setList(InvestmentToResp.convertList(systemInvestment.getList(), merchantNameMap, address));

        return result;
    }

    /**
     * 删除招商项目
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteInvestment(List<Integer> ids) {
        LambdaQueryWrapper<SystemInvestment> queryWrapper = new LambdaQueryWrapper<>();
        return investmentMapper.delete(queryWrapper.in(SystemInvestment::getId, ids));
    }

    /**
     * 更新招商项目
     *
     * @param
     * @return
     */
    @Override
    public int updateInvestment(InvestmentUpdateReqVO investmentUpdateReqVO) throws IOException {
        SystemInvestment investment = BeanUtils.toBean(investmentUpdateReqVO, SystemInvestment.class);
        return investmentMapper.updateInvestment(investment);
    }

    /**
     * 新增招商项目
     *
     * @param
     * @return
     */
    @Override
    public int createInvestment(InvestmentInsertReqVO investmentInsertReqVO) throws IOException {
        SystemInvestment systemInvestment = BeanUtils.toBean(investmentInsertReqVO, SystemInvestment.class);
        systemInvestment.setMerchantId(WebFrameworkUtils.getLoginUserId());
        return investmentMapper.insert(systemInvestment);
    }

    @Override
    public SystemInvestment getInvestmentInfo(Long id) {
        return investmentMapper.selectById(id);
    }

    @Override
    public PageResult<SystemInvestment> getInvestmentPageResult(AppInvestmentPageReqVO pageVO) {
        System.out.println(pageVO.getName());
        if(!Objects.isNull(pageVO.getName()) || !Objects.isNull(pageVO.getProduceArea()) || !Objects.isNull(pageVO.getProductType())){
            return investmentMapper.selectPageResult(pageVO);
        }
        return investmentMapper.selectByRandom();
    }

}


